package com.taiyuan.tongcheng.ui;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import org.kymjs.kjframe.KJHttp;
import org.kymjs.kjframe.http.HttpCallBack;
import org.kymjs.kjframe.http.HttpConfig;
import org.kymjs.kjframe.http.HttpParams;
import org.kymjs.kjframe.ui.BindView;
import org.kymjs.kjframe.utils.KJLoger;
import org.kymjs.kjframe.utils.StringUtils;
import org.kymjs.kjframe.utils.SystemTool;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.taiyuan.tongcheng.application.TCBroadcastAction;
import com.taiyuan.tongcheng.bean.UserInfo;
import com.taiyuan.tongcheng.db.TcDbHelper;
import com.taiyuan.tongcheng.http.HttpParamUtil;
import com.taiyuan.tongcheng.http.TcWebservice;
import com.taiyuan.tongcheng.http.WebData;
import com.taiyuan.tongcheng.jsonparser.TcJsParser;
import com.tytc56.user.R;

public class SettingInfoActivity extends BaseActivity {
	@BindView(id = R.id.btn_setting_ok, click = true)
	private Button submitBtn;
	@BindView(id = R.id.etv_ip)
	private EditText userNameEtv;

	public static final int LIVE_OK = 0x3;

	@Override
	public void setRootView() {
		setContentView(R.layout.activity_setting_info);
	}

	
	@Override
	public void widgetClick(View v) {
		super.widgetClick(v);
		switch (v.getId()) {
		case R.id.btn_setting_ok:
			if (checkInput()) {
				updataName(userNameEtv.getText().toString());
			}
			break;

		default:
			break;
		}
	}

	@Override
	public void initWidget() {
		super.initWidget();
		initView();
		topTv.setText("信息修改");
		rightTv.setVisibility(View.GONE);
	}

	private boolean checkInput() {
		if (StringUtils.isEmpty(userNameEtv.getText())) {

			return false;
		}
		
		if(!SystemTool.checkNet(getApplicationContext())){
			return false;
		}
		
		return true;
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		setResult(LIVE_OK);
	}
	
	/**
	 * 
	* @Description: 更改信息
	* @param name
	* @return void
	* @throws
	* @autour wzq
	* @date 2015-9-24 下午2:00:49
	* @update (date)
	 */
	private void updataName(final String name) {
		TcLoaddingDialog.show();
		HttpConfig config = new HttpConfig();
		config.cacheTime = 0;
		KJHttp kjHttp = new KJHttp(config);
        HttpParams params =new HttpParams();
        params=HttpParamUtil.getAccountUdpateUserName(getApplicationContext(),name );
        KJLoger.debug(params.getUrlParams().toString());
        kjHttp.post(TcWebservice.WEBSERVER_IP+TcWebservice.API.ACCOUNT_UPDATA_NAME, params, new HttpCallBack() {
			@Override
			public void onFailure(int errorNo, String strMsg) {
				super.onFailure(errorNo, strMsg);
			}
			@Override
			public void onSuccess(String t) {
				try {
					String date=URLDecoder.decode(t, "UTF-8");
					KJLoger.debug(date);
					WebData webdata = TcJsParser.getWedataData(date); 
					if(webdata.getStatus()==1){
						showCustomToast("更改信息成功");
						UserInfo info = TcJsParser.getUserInfo(webdata
								.getData());
						TcDbHelper.saveUser(SettingInfoActivity.this, info);
						sendBroadcast(new Intent(TCBroadcastAction.SUCCESS_LOGIN));
						spUtil.setLoginFlag(1);
						getIntent().putExtra("name", name);
						setResult(RESULT_OK, getIntent());
						SettingInfoActivity.this.finish();
					}else{
						showErroCustomToast("更新失败");
					}
					 if(TcLoaddingDialog.isShowing()){
						  TcLoaddingDialog.cancel();
					  }
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
				super.onSuccess(t);
			}
        });
	
	}

}
